<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;
use think\Db;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Userrepay extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }

    /**
     * @return \think\response\View
     * 还款列表
     */
    public function userrepayList()
    {
        $query=[];
        if(session('role') == 'admin'){
            $query['user_phone']=["NOT LIKE",'1%'];
        }else if(session('role') == 'testing'){
            $query['user_phone']=["LIKE",'1%'];
        }
        if(isset($_GET['userPhone']) && !empty($_GET['userPhone'])){
           $query['user_phone']=$_GET['userPhone'];
        }

        if(isset($_GET['repay_status']) && $_GET['repay_status']!=11){
           $query['repay_status']=$_GET['repay_status'];
        }
        if(isset($_GET['commit_status']) && $_GET['commit_status']!=11){
           $query['commit_status']=$_GET['commit_status'];
        }

        if(isset($_GET['upAmount']) && isset($_GET['downAmount']) && !empty($_GET['upAmount']) && !empty($_GET['downAmount'])){
            $query['repay_money'][]=['EGT',$_GET['upAmount']];
            $query['repay_money'][]=['ELT',$_GET['downAmount']];
        }else if(isset($_GET['upAmount']) && !empty($_GET['upAmount'])){
        $query['repay_money']=['EGT',$_GET['upAmount']];
         }else if(isset($_GET['downAmount']) && !empty($_GET['downAmount'])){
        $query['repay_money']=['ELT',$_GET['downAmount']];
         }
        if(isset($_GET['upTime']) && isset($_GET['downTime']) && !empty($_GET['upTime']) && !empty($_GET['downTime'])){
            $query['update_time'][]=['EGT',$_GET['upTime']];
            $query['update_time'][]=['ELT',$_GET['downTime']];
        }else if(isset($_GET['upTime']) && !empty($_GET['upTime'])){
        $query['update_time']=['EGT',$_GET['upTime']];
         }else if(isset($_GET['downTime']) && !empty($_GET['downTime'])){
        $query['update_time']=['ELT',$_GET['downTime']];
         }

        $data=Db::table("userrepay")
            ->where($query)
            ->order("update_time DESC")
            ->paginate(15);
        $totalCount=Db::table("userrepay")
            ->where($query)
            ->count();

        $totalMoney=Db::table("userrepay")
            ->where($query)
            ->sum('repay_money');


        if(isset($_GET['extend']) && $_GET['extend'] == 1){
            $column_name=[
                [
                    'Mobile',
                    'Loan ID',
                    'Customer ID',
                    'KB Customer ID',
                    'KB Loan ID',
                    'Repayment Amount',
                    'Repayment Date',
                    'UTR/PG reference',
                    'Razorpay Fee',
                    'GST',
                    'total Fee',
                ]
            ];
            $totalCount=Db::table("userrepay")
                ->where($query)
                ->order("update_time DESC")
                ->select();
            $extendData=[];
            foreach($totalCount as $k=>$v){
                $payedId=Db::table('userrepaytransaction')
                     ->where('id',$v['replay_transaction_id'])
                    ->field('payed_id,payed_info')
                    ->find();
                $payInfo=json_decode($payedId['payed_info']);
                $repayDate=date("d-m-Y",strtotime($v['create_time']));
                $extendData[]=[
                    "Mobile"=>$v['user_phone'],
                    "Loan ID"=>substr($v['user_loan_id'],0,30),
                    "Customer ID"=>substr($v['user_id'],0,30),
                    "KB Customer ID"=>'FCSH-'.substr($v['user_loan_id'],0,30),
                    "KB Loan ID"=>'FCSH-'.substr($v['user_id'],0,30),
                    "Repayment Amount"=>number_format($v['repay_money']/100,2),
                    "Repayment Date"=>$repayDate,
                    "UTR/PG reference"=>$payedId['payed_id'],
                    "Razorpay Fee"=> isset($payInfo->fee)?number_format(($payInfo->fee-$payInfo->tax)/100,2):0,
                    "GST"=>isset($payInfo->tax)?number_format($payInfo->tax/100,2):0,
                    "total Fee"=>isset($payInfo->fee)?number_format($payInfo->fee/100,2):0,
                ];
            }

            $data = array_merge($column_name, $extendData);
            $this->extendToExcel($data);
        }
         $page = $data->render();
         $this->assign('page',$page);
        $newData=[];
//        dd($data);
        foreach ($data as $k=>$v) {
            $newData[]=[
                "id"=>$v['id'],
                "user_id"=>$v['user_id'],
                "user_phone"=>$v['user_phone'],
                "user_loan_id"=>$v['user_loan_id'],
                "product_id"=>$v['product_id'],
                "commit_status"=>$v['commit_status'],
                "repay_money"=>$v['repay_money'],
                "repay_type"=>$v['repay_type'],
                "repay_status"=>$v['repay_status'],
                "replay_transaction_id"=>$v['replay_transaction_id'],
                "create_time"=>$this->gmtToIndiaTimeByMysql($v['create_time']),
               "update_time"=>$this->gmtToIndiaTimeByMysql($v['update_time'])
            ];
        }
        $this->assign('data',$data);
         $this->assign('totalCount',$totalCount);
         $this->assign('totalMoney',$totalMoney);
        return view();
    }
    /**
     * 查看详情
     */
    public function repayInfo(){
        $id=$_GET['uid'];
        //查询借款信息
        $repayUrl=Uri::HOST.Uri::SELECTUSERREPAY."?id=".$id;
        $repayInfo=$this->client->getArrayData($repayUrl);
        $repayInfo=$repayInfo['data'][0];
//        dd($repayInfo);
        //查询用户信息
        $userUrl=Uri::HOST.Uri::USERLIST."?id=".$repayInfo['user_id'];
        $userInfo=$this->client->getArrayData($userUrl);
        $userInfo=$userInfo['data'][0];
//        dd($userInfo);
        //查询借款信息
        $userLoanUrl=Uri::HOST.Uri::SELECTUSERLOAN.'?id='.$repayInfo['user_loan_id'];
        $userLoanInfo=$this->client->getArrayData($userLoanUrl);
        $userLoanInfo=$userLoanInfo['data'][0];
//        dd($userLoanInfo);
        $this->assign('repayInfo',$repayInfo);
        $this->assign('user',$userInfo);
        $this->assign('userLoan',$userLoanInfo);
        return view();
    }

}